package com.aj.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.aj.activity.fragment.FamilyBaseFragment;
import com.aj.activity.fragment.FamilyPhotoFragment;
import com.aj.activity.fragment.PersonPhotoFragment;

public class HomesMemoryActivity extends TitleBarBaseActivity {

    private ViewPager mViewPager; // 页卡内容
    private ImageView imageView; // 动画图片
    private TextView tab_person_photo;
    private TextView tab_family_photo;
    private int offset = 0; // 动画图片偏移量
    private int currIndex = 0; // 当前页卡编号
    private int bmpW; // 动画图片宽度

    public static void newInstance(Context context) {
        Intent intent = new Intent(context, HomesMemoryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homes_memory_layout);
        initPageTitleBars();
        initViews();
        initImageView();
        initViewPager();
    }

    private void initViewPager() {
        tab_person_photo.setOnClickListener(new CustomOnClickListener(0));
        tab_family_photo.setOnClickListener(new CustomOnClickListener(1));
        FragmentAdapter adapter = new FragmentAdapter(initFragment(), getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(new CustomOnPageChangeListener());

    }

    private void initViews() {
        imageView = (ImageView) findViewById(R.id.photo_cursor);
        mViewPager = (ViewPager) findViewById(R.id.familyViewPager);
        tab_person_photo = (TextView) findViewById(R.id.tab_person_photo);
        tab_family_photo = (TextView) findViewById(R.id.tab_family_photo);
    }

    class CustomOnClickListener implements View.OnClickListener {

        private int index = 0;

        public CustomOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mViewPager.setCurrentItem(index);
        }
    }

    private void changTabTextColor(int index) {
        if (index == 0) {
            tab_person_photo.setTextColor(Color.parseColor("#ce3c3d"));
            tab_family_photo.setTextColor(Color.parseColor("#333333"));
        } else {
            tab_person_photo.setTextColor(Color.parseColor("#333333"));
            tab_family_photo.setTextColor(Color.parseColor("#ce3c3d"));
        }
    }

    private List<FamilyBaseFragment> initFragment() {
        List<FamilyBaseFragment> fragments = new ArrayList<FamilyBaseFragment>();

        FamilyBaseFragment deviceFragment = new PersonPhotoFragment();
        fragments.add(deviceFragment);

        FamilyBaseFragment dataFragment = new FamilyPhotoFragment();
        fragments.add(dataFragment);

        return fragments;
    }

    class FragmentAdapter extends FragmentPagerAdapter {

        private List<FamilyBaseFragment> mFragments;

        public FragmentAdapter(List<FamilyBaseFragment> fragments, FragmentManager fm) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }

    class CustomOnPageChangeListener implements ViewPager.OnPageChangeListener {

        int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Animation animation = new TranslateAnimation(one * currIndex, one * position, 0, 0);
            currIndex = position;
            changTabTextColor(currIndex);
            animation.setFillAfter(true);
            animation.setInterpolator(new LinearInterpolator());
            animation.setDuration(200);
            imageView.startAnimation(animation);
        }

    }

    private void initPageTitleBars() {
        setTitle(R.string.home_memory_text);
        TextView backButton = getTitleLeftButton();
        backButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initImageView() {

        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.tab_bottom_border).getWidth();// 获取图片宽度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        imageView.setImageMatrix(matrix);// 设置动画初始位置
    }

}
